package cn.iocoder.an.module.business.controller.admin.maintenanceclassification;

import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.maintenanceclassification.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.maintenanceclassification.MaintenanceClassificationDO;
import cn.iocoder.an.module.business.service.maintenanceclassification.MaintenanceClassificationService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 维修分类")
@RestController
@RequestMapping("/business/maintenance-classification")
@Validated
public class MaintenanceClassificationController {

    @Resource
    private MaintenanceClassificationService maintenanceClassificationService;

    @PostMapping("/create")
    @Operation(summary = "创建维修分类")
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:create')")
    public CommonResult<Long> createMaintenanceClassification(@Valid @RequestBody MaintenanceClassificationSaveReqVO createReqVO) {
        return success(maintenanceClassificationService.createMaintenanceClassification(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维修分类")
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:update')")
    public CommonResult<Boolean> updateMaintenanceClassification(@Valid @RequestBody MaintenanceClassificationSaveReqVO updateReqVO) {
        maintenanceClassificationService.updateMaintenanceClassification(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维修分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:delete')")
    public CommonResult<Boolean> deleteMaintenanceClassification(@RequestParam("id") Long id) {
        maintenanceClassificationService.deleteMaintenanceClassification(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除维修分类")
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:delete')")
    public CommonResult<Boolean> deleteMaintenanceClassificationList(@RequestParam("ids") List<Long> ids) {
        maintenanceClassificationService.deleteMaintenanceClassificationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:query')")
    public CommonResult<MaintenanceClassificationRespVO> getMaintenanceClassification(@RequestParam("id") Long id) {
        MaintenanceClassificationDO maintenanceClassification = maintenanceClassificationService.getMaintenanceClassification(id);
        return success(BeanUtils.toBean(maintenanceClassification, MaintenanceClassificationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维修分类分页")
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:query')")
    public CommonResult<PageResult<MaintenanceClassificationRespVO>> getMaintenanceClassificationPage(@Valid MaintenanceClassificationPageReqVO pageReqVO) {
        PageResult<MaintenanceClassificationDO> pageResult = maintenanceClassificationService.getMaintenanceClassificationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaintenanceClassificationRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有维修分类")
    public CommonResult<List<MaintenanceClassificationRespVO>> getAllList() {
        List<MaintenanceClassificationDO> list = maintenanceClassificationService.getAllList();
        return success(BeanUtils.toBean(list, MaintenanceClassificationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维修分类 Excel")
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaintenanceClassificationExcel(@Valid MaintenanceClassificationPageReqVO pageReqVO,
                                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaintenanceClassificationDO> list = maintenanceClassificationService.getMaintenanceClassificationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维修分类.xls", "数据", MaintenanceClassificationRespVO.class,
                BeanUtils.toBean(list, MaintenanceClassificationRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得设维修分类导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<MaintenanceClassificationImportExcelVO> list = Arrays.asList(
                MaintenanceClassificationImportExcelVO.builder().name("保养").status(MaintenanceStatusEnum.run.getStatus()).description("正常保养").build()
        );
        // 输出
        ExcelUtils.write(response, "维修分类导入模板.xlsx", "维修分类列表", MaintenanceClassificationImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入维修分类数据")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('business:maintenance-classification:import')")
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<MaintenanceClassificationImportExcelVO> list = ExcelUtils.read(file, MaintenanceClassificationImportExcelVO.class);
        return success(maintenanceClassificationService.importMaintenanceClassificationList(list, updateSupport));
    }

}